package com.qf.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.qf.feign.SearchFegin;
import com.qf.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * @author Wy
 * @version V1.0
 * @Project MavenDemo
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/7/6 19:08
 */
@RestController
public class TestController {

    /**
     * restTemplate是Spring封装的java服务端发送http协议的远程调用工具.
     */
    @Autowired
    private  EurekaClient eurekaClient;

    @Autowired
    private  RestTemplate restTemplate;
    @Autowired
    private SearchFegin searchFegin;

    @RequestMapping("/consumer")
    public String consumer(){
        //1. 通过eurekaClient对象获取search搜索微服务的对象信息
        InstanceInfo info = eurekaClient.getNextServerFromEureka("QF-SEARCH", false);

        //2. 从搜索微服务对象信息中获取搜索微服务的ip和端口号
        String url = info.getHomePageUrl();
        System.out.println(url);
        //3. 通过restTemplate对象发送http协议远程调用
        String forObject = restTemplate.getForObject( "http://QF-SEARCH/search/test", String.class);

        return  forObject;
    }

    @RequestMapping("/testFegin")
    public  String testFegin(){
        String s = searchFegin.test1();
        System.out.println(s);
        return  s;
    }
     @GetMapping("/customer/{id}")
    public  Object testfindById(@PathVariable Integer id){
         return searchFegin.findById(id);

    }
     @GetMapping("/idname")
    public  Object findByIdAndName(@RequestParam Integer id, @RequestParam String name){
        return  searchFegin.findByIdAndName(id, name);
    }

     @GetMapping("/save1")
    public  User save(User user){
        return   searchFegin.save(user);
    }


}
